package com.ck.demo;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.IOException;
import java.io.StringReader;
import java.util.*;

/**
 * 分词测试
 * @author ck
 */
public class IKAnalyerDemo {

    public static void main(String[] args) {
        String text = "IK Analyzer是一个开源的，基于java语言"
                + "开发的轻量级的中文分词工具包。从2006年12月推出1.0版开始"
                + "IKAnalyzer已经推出了4个大版本。最初，它是以开源项目Luence为"
                + "应用主体的，结合词典分词和文法分析算法的中文分词组件。从3.0版"
                + "本开始，IK发展为面向Java的公用分词组件，独立于Lucene项目，同时"
                + "提供了对Lucene的默认优化实现";
        String str = "是在播放单田芳的西游记029了";
        test2(text);
        test3();
    }

    private static void test(String str) {
        Analyzer analyzer = new IKAnalyzer(true);
        StringReader reader = new StringReader(str);
        try {
            TokenStream ts = analyzer.tokenStream("", reader);
            CharTermAttribute term = ts.getAttribute(CharTermAttribute.class);

            while (ts.incrementToken()) {
                System.out.print(term.toString() + "|");
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void test2(String str) {
        try {
            StringReader sr = new StringReader(str);
            IKSegmenter ik = new IKSegmenter(sr, true);
            Lexeme lex;
            while((lex = ik.next()) != null){
                System.out.println("type:"+lex.getLexemeTypeString()+"-"+lex.getLexemeText());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void test3(){
        Map<String, Integer> map = new HashMap();
        map.put("1", 8);
        map.put("2", 12);
        map.put("3", 53);
        map.put("4", 33);
        map.put("5", 11);
        map.put("6", 53);
        map.put("7", 3);
        Set<String> set=map.keySet();
        map.entrySet();
        List<Map.Entry<String,Integer>> list = new ArrayList(map.entrySet());
        Collections.sort(list, (o1, o2) -> (o2.getValue() - o1.getValue()));
        System.out.println(list.get(0).getKey());
    }
}
